package com.ruoyi.brs.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brs.mapper.BrsRentalOrderMapper;
import com.ruoyi.brs.domain.BrsRentalOrder;
import com.ruoyi.brs.service.IBrsRentalOrderService;

/**
 * 租赁订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@Service
public class BrsRentalOrderServiceImpl implements IBrsRentalOrderService 
{
    @Autowired
    private BrsRentalOrderMapper brsRentalOrderMapper;

    /**
     * 查询租赁订单管理
     * 
     * @param id 租赁订单管理主键
     * @return 租赁订单管理
     */
    @Override
    public BrsRentalOrder selectBrsRentalOrderById(Long id)
    {
        return brsRentalOrderMapper.selectBrsRentalOrderById(id);
    }

    /**
     * 查询租赁订单管理列表
     * 
     * @param brsRentalOrder 租赁订单管理
     * @return 租赁订单管理
     */
    @Override
    public List<BrsRentalOrder> selectBrsRentalOrderList(BrsRentalOrder brsRentalOrder)
    {
        return brsRentalOrderMapper.selectBrsRentalOrderList(brsRentalOrder);
    }

    /**
     * 新增租赁订单管理
     * 
     * @param brsRentalOrder 租赁订单管理
     * @return 结果
     */
    @Override
    public int insertBrsRentalOrder(BrsRentalOrder brsRentalOrder)
    {
        brsRentalOrder.setCreateTime(DateUtils.getNowDate());
        return brsRentalOrderMapper.insertBrsRentalOrder(brsRentalOrder);
    }

    /**
     * 修改租赁订单管理
     * 
     * @param brsRentalOrder 租赁订单管理
     * @return 结果
     */
    @Override
    public int updateBrsRentalOrder(BrsRentalOrder brsRentalOrder)
    {
        brsRentalOrder.setUpdateTime(DateUtils.getNowDate());
        return brsRentalOrderMapper.updateBrsRentalOrder(brsRentalOrder);
    }

    /**
     * 批量删除租赁订单管理
     * 
     * @param ids 需要删除的租赁订单管理主键
     * @return 结果
     */
    @Override
    public int deleteBrsRentalOrderByIds(Long[] ids)
    {
        return brsRentalOrderMapper.deleteBrsRentalOrderByIds(ids);
    }

    /**
     * 删除租赁订单管理信息
     * 
     * @param id 租赁订单管理主键
     * @return 结果
     */
    @Override
    public int deleteBrsRentalOrderById(Long id)
    {
        return brsRentalOrderMapper.deleteBrsRentalOrderById(id);
    }

    @Override
    public void updateUserRental() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String oldTime = LocalDateTime.now().minus(Period.ofMonths(2)).format(formatter);
        List<BrsRentalOrder> brsRentalOrders =  brsRentalOrderMapper.selectAllBrsRentalOrder(oldTime);
        //筛选过期订单
        LocalDateTime expirationTime  = LocalDateTime.now().minus(Period.ofMonths(1));
        List<Long> expirationList = new ArrayList<Long>();
        List<Long>  expirationUserList = new ArrayList<Long>();
        for(BrsRentalOrder item : brsRentalOrders){
            if(LocalDateTime.ofInstant(item.getRentalTime().toInstant(),ZoneId.systemDefault()).isAfter(expirationTime)){
                expirationList.add(item.getId());
                expirationUserList.add(item.getUserId());
            }
        }

        //todo 发消息给用户

        brsRentalOrderMapper.updateBrsRentalOrderList(expirationList);

    }
}
